package com.synjones.ui.recognize;


import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.synjones.entry.SignPerson;
import com.synjones.face.camera.FaceHelper;
import com.synjones.face.camera.FaceRectView;
import com.synjones.idcardusb_reader.R;
import com.synjones.ui.FirstScreenRouter;
import com.synjones.ui.MainActivity;
import com.synjones.ui.check.CheckFragment;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecognizeFragment extends Fragment implements RecognizeContract.View{

    private TextView recognizeName;
    private TextView recognizeSex;
    private TextView recognizeWork;
    private TextView recognizeHotel;
    private TextView recognizeRoom;
    private TextView recognizeScore;
    private ImageView recognizeCardFace;
    private ImageView recognizeFrame;
    private ImageView recognizeIdCard;
    private Button recognizeCancel;

    private TextureView cameraPreview;
    private FaceRectView fcaceRect;
    private FaceHelper faceHelper;

    private FirstScreenRouter main;

    private RecognizeContract.Presenter presenter;

    public RecognizeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recongnize, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        main = (FirstScreenRouter) getActivity();
        faceHelper = new FaceHelper(cameraPreview, fcaceRect, getContext(),getActivity());
        presenter = new RecognizePresenter(this,getContext(),faceHelper);
        presenter.start();
    }

    private void initView(View view){
        recognizeName = (TextView) view.findViewById(R.id.recognizeName);
        recognizeSex = (TextView) view.findViewById(R.id.recognizeSex);
        recognizeScore = (TextView) view.findViewById(R.id.recognizeScore);
        recognizeWork = (TextView) view.findViewById(R.id.recognizeWork);
        recognizeHotel = (TextView) view.findViewById(R.id.recognizeHotel);
        recognizeRoom = (TextView) view.findViewById(R.id.recognizeRoom);
        recognizeCardFace = (ImageView) view.findViewById(R.id.recognizeCardFace);
        recognizeFrame = (ImageView) view.findViewById(R.id.recognizeFrame);
        recognizeIdCard = (ImageView) view.findViewById(R.id.recognizeIdCard);
        cameraPreview = (TextureView) view.findViewById(R.id.cameraPreview);
        fcaceRect = (FaceRectView) view.findViewById(R.id.faceRect);
        recognizeCancel = (Button) view.findViewById(R.id.recognizeCancel);
        recognizeCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main.gotoWelcome();
            }
        });
    }

    @Override
    public void showCardInfo(final String name, final String sex, final String work, final String hotel, final String room, final Bitmap cardFace) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recognizeName.setText(name);
                recognizeSex.setText(sex);
                recognizeWork.setText(work);
                recognizeHotel.setText(hotel);
                recognizeRoom.setText(room);
                recognizeCardFace.setImageBitmap(cardFace);
            }
        });
    }

    @Override
    public void showCompareInfo(final Bitmap frameFace, final Bitmap cardFace) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recognizeFrame.setImageBitmap(frameFace);
                recognizeIdCard.setImageBitmap(cardFace);
                if (frameFace!=null){
                    recognizeScore.setTextColor(Color.GREEN);
                    recognizeScore.setText("匹配");
                }else{
                    recognizeScore.setTextColor(Color.RED);
                    recognizeScore.setText("不匹配");
                }
            }
        });

    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void resetUI() {
        recognizeName.setText("");
        recognizeSex.setText("");
        recognizeWork.setText("");
        recognizeHotel.setText("");
        recognizeRoom.setText("");
        recognizeScore.setText("对比结果");
        recognizeScore.setTextColor(getResources().getColor(R.color.infoTextColor,null));
        recognizeCardFace.setImageBitmap(null);
        recognizeFrame.setImageBitmap(null);
        recognizeIdCard.setImageBitmap(null);
    }

    @Override
    public void showBackInfo(String msg, int color) {
        main.showToast(msg,color);
    }

    @Override
    public void isSelfCheckIn(boolean selfCheckIn) {
        presenter.setSelfCheckIn(selfCheckIn);
    }

    @Override
    public void goCheckPage(SignPerson signPerson) {
        main.gotoCheck(signPerson.getInvitationCode(),CheckFragment.DO_SIGN,signPerson);
    }

    @Override
    public void goOtherPage(final int doWhat, final List<SignPerson> data, final String idno,String idname) {
        main.gotoOthers(doWhat,data,idno,idname);
    }

    @Override
    public void pauseScan() {
        presenter.pauseScan();
    }

    @Override
    public void resumeScan() {
        presenter.resumeScan();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.release();
    }
}
